﻿#include <QThread>
#include <QEventLoop>
#include <QTimer>
#include <QFile>
#include <QCoreApplication>
#include <QDir>
#include "testNet.h"

TestFileTrans::TestFileTrans()
{
    JZNetPackInit();
    JZNetFileTrans::registPack();
}

void TestFileTrans::startServer()
{
    QString test_root = QCoreApplication::instance()->applicationDirPath() + "/test";
    QString server_root = test_root + "/server";

    QDir dir;
    if (!dir.exists(server_root))
        dir.mkpath(server_root);

    genFile(server_root + "/1.txt");

    QThread* server_thread = new QThread();

    QObject* server_obj = new QObject(server_thread);
    JZNetServer* server = new JZNetServer(server_obj);
    JZNetFileTrans* server_file = new JZNetFileTrans(server_obj);
    server_file->setServer(server);
    server->connect(server, &JZNetServer::sigNetPackRecv, [server,server_file](int net_id, JZNetPackPtr ptr) {
        if (JZNetFileTrans::isFilePack(ptr->type()))
            server_file->recvPack(net_id, ptr);
        else
            server->sendPack(net_id, ptr.data());
    });
    server->startServer(8889);
    server_thread->moveToThread(server_thread);
    server->connect(server, &JZNetServer::sigNewConnect, [server,server_file](int net_id) {
        auto t1 = server_file->startPull(net_id, "1.txt", "server_pull.txt");
        auto t2 = server_file->startPush(net_id, "1.txt", "server_push.txt");    
    });
    server_file->connect(server_file, &JZNetFileTrans::sigTaskFinish, this, &TestFileTrans::onTaskFinish);
    server_file->setDir(server_root);

    server_thread->start();
}

void TestFileTrans::startClient()
{
    QString test_root = QCoreApplication::instance()->applicationDirPath() + "/test";
    QString client_root = test_root + "/client";
    QDir dir;
    if (!dir.exists(client_root))
        dir.mkpath(client_root);

    genFile(client_root + "/1.txt");
    JZNetClient* client = new JZNetClient(this);
    JZNetFileTrans* client_file = new JZNetFileTrans(client);
    client_file->setClient(client);
    client->connect(client, &JZNetClient::sigNetPackRecv, [client_file](JZNetPackPtr ptr) 
    {   
        if (JZNetFileTrans::isFilePack(ptr->type()))
        {
            client_file->recvPack(1, ptr);
        }
    });
    client_file->connect(client_file, &JZNetFileTrans::sigTaskFinish, this, &TestFileTrans::onTaskFinish);

    client_file->setDir(client_root);
    QTimer::singleShot(0, [client, client_file] {
        bool ret = client->connectToHost("127.0.0.1", 8889);
        Q_ASSERT(ret);

        for (int i = 0; i < 5; i++)
        {
            auto t1 = client_file->startPull(1, "1.txt", "client_pull_" + QString::number(i) + ".txt");
            auto t2 = client_file->startPush(1, "1.txt", "client_push_" + QString::number(i) + ".txt");
        }
    });
}

void TestFileTrans::testFileTrans()
{
    startServer();
    startClient();
}

void TestFileTrans::genFile(QString path)
{
    QFile file(path);
    if (file.open(QIODevice::WriteOnly))
    {
        file.write(path.toUtf8());
        file.close();
    }
}

void TestFileTrans::onTaskFinish(JZNetFileTaskPtr task)
{
    if (task->type != JZNetFileTask::Recv)
        return;

    QString file_path = task->filePath;
    bool ok = !task->error && checkFile(file_path);
    qDebug() << ok << "," << file_path;
}

bool TestFileTrans::checkFile(QString path)
{
    bool check_ok = false;
    QFile file(path);
    if (file.open(QIODevice::ReadOnly))
    {
        QByteArray buffer = file.readAll();
        file.close();

        check_ok = (buffer.size() > 10);
    }
    return check_ok;
}

int main(int argc, char* argv[])
{
    QCoreApplication app(argc, argv);


    return app.exec();
}